package gu.sql2java.excel.config;

import java.util.List;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableBiMap.Builder;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Strings.nullToEmpty;
import static net.gdface.utils.MiscellaneousUtils.elementsOf;

public class MapExpression {
	private final static LoadingCache<String, ImmutableBiMap<String, String>> cache = CacheBuilder.newBuilder()
			.build(new CacheLoader<String, ImmutableBiMap<String, String>>() {

				@Override
				public ImmutableBiMap<String, String> load(String key) throws Exception {
					return parseExp(key);
				}
			});
	private static ImmutableBiMap<String, String> parseExp(String converterExp){
		converterExp = nullToEmpty(converterExp).replaceAll("\\s*", "");
		Builder<String, String> builder = ImmutableBiMap.builder();
		if(!isNullOrEmpty(converterExp)){
			String[] convertSource = converterExp.split(",");
			for (String item : convertSource)
			{
				String[] itemArray = item.split("[=:-]");
				if(itemArray.length == 2){
					String left = itemArray[0].trim();
					String right = itemArray[1].trim();
					if(!left.isEmpty()&& !right.isEmpty()){
						builder.put(left, right);
					}
				}
			}
		}
		return builder.build();
	}
	
    /**
     * 解析导出值 0=男,1=女,2=未知
     * 
     * @param propertyValue 参数值
     * @param converterExp 翻译注解
     * @param separator 分隔符
     * @return 解析后值
     */
    public static Object convertByExp(Object propertyValue, String converterExp, String separator)
    {
    	if(null != propertyValue){
    		String value=String.valueOf(propertyValue);
    		converterExp = nullToEmpty(converterExp).replaceAll("\\s*", "");
    		if(isNullOrEmpty(separator)){
    			separator = ",";
    		}
    		ImmutableBiMap<String, String> m = cache.getUnchecked(converterExp);
    		if(!m.isEmpty()){
    			List<String> transformed = Lists.transform(elementsOf(value,separator),e->{
    				String v = m.get(e);
    				/** 如果没有找到匹配的值返回通配符定义的值,如果没定义通配值则返回原值 */
    				if(null == v){
    					v = m.get("*");
    				}
    				return null == v ? e : v;
    			});
    			Iterable<String> filtered = Iterables.filter(transformed, Predicates.notNull());
    			return Joiner.on(separator).join(filtered);    		
    		}
    	}
    	return propertyValue;
    }

    /**
     * 反向解析值 男=0,女=1,未知=2
     * 
     * @param propertyValue 参数值
     * @param converterExp 翻译注解
     * @param separator 分隔符
     * @return 解析后值
     */
    public static Object reverseByExp(Object propertyValue, String converterExp, String separator)
    {
    	if(null != propertyValue){
    		String value=String.valueOf(propertyValue);
    		converterExp = nullToEmpty(converterExp).replaceAll("\\s*", "");
    		if(isNullOrEmpty(separator)){
    			separator = ",";
    		}
    		ImmutableBiMap<String, String> m = cache.getUnchecked(converterExp).inverse();
    		if(!m.isEmpty()){
    			List<String> transformed = Lists.transform(elementsOf(value,separator),e->m.get(e));
    			Iterable<String> filtered = Iterables.filter(transformed, Predicates.notNull());
    			return Joiner.on(separator).join(filtered);
    		}
    	}
    	return propertyValue;
    }
}
